package com.yedou.produce;

import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Random;

//@Component
//@RocketMQTransactionListener(txProducerGroup = "tx-producer-group")
//public class MyTransactionCheckListener implements RocketMQLocalTransactionListener {
//
//    public static final Logger LOGGER = LoggerFactory.getLogger(MyTransactionCheckListener.class);
//
//    Random random = new Random();
//
//    @Override
//    public RocketMQLocalTransactionState executeLocalTransaction(org.springframework.messaging.Message msg, Object arg) {
//        //执行业务，保存本地事务
//
//        //保存成功
//        if(random.nextInt() % 2 == 0){
//            LOGGER.info("本地事务提交成功...");
//            return RocketMQLocalTransactionState.COMMIT;
//
//        }
//        LOGGER.info("本地事务提交未知...");
//        return RocketMQLocalTransactionState.UNKNOWN;
//    }
//
//    @Override
//    public RocketMQLocalTransactionState checkLocalTransaction(org.springframework.messaging.Message msg) {
//        //这里查询本地事务状态
//        if(random.nextInt() % 2 == 0){
//            LOGGER.info("本地事务回查...COMMIT");
//            return RocketMQLocalTransactionState.COMMIT;
//        }
//        LOGGER.info("本地事务回查...ROLLBACK");
//        return RocketMQLocalTransactionState.ROLLBACK;
//    }
//}
